<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="GeoCodeLookup.aspx.cs" Inherits="Instedd.Labs.GeoBlogging.WebApplication.GeoCodeLookup" %>

<html xmlns="http://www.w3.org/1999/xhtml" >
<head>
    <title>InSTEDD | GeoCode Lookup</title>
    <link href="css/main.css" rel="stylesheet" type="text/css" />
    <script src="http://maps.google.com/maps?file=api&amp;v=2&amp;key=<%= ConfigurationManager.AppSettings["GoogleMapsKey"].ToString() %>"
      type="text/javascript"></script>
    <script type="text/javascript">

    //<![CDATA[
    function loadMap(x,y) {
      if (GBrowserIsCompatible()) {
        var map = new GMap2(document.getElementById("map"));
        map.setCenter(new GLatLng(x, y), 13);
      }
    }
    </script>

</head>
<body style="padding-left:10px;padding-top:10px">
    <form id="form1" runat="server">
    <div class="label" style="font-size:x-small;color:black">
    You don't know your Lat/Long?<br /><br />Don't worry, we'll try to find it out for you.
    <br /><br />
    Enter your postal address here: <input type="text" id="postalAddress" style="width:330px" /><br />
    <input type="button" value="Search" onclick="PostalAddress2LatLong();" />
    <br /><br />
    <div class="label" style="font-size:x-small;color:black" id="resultMessage"></div>
    <div class="label" style="display:none;font-size:x-small;color:black" id="resultMessage2"><br />
    You can <a href="#" onclick="CloseForm();">click here</a> to close this dialog and we'll fill the previous fields for you.</div><br />
    <div class="label" style="display:none;font-size:x-small;color:black" id="mapNotice">Also, please check in the map if our guess was accurate:</div>
    <div id="map" style="display:none;width: 400px; height: 200px; border-left-color: green; border-bottom-color: green; border-top-style: solid; border-top-color: green; border-right-style: solid; border-left-style: solid; border-right-color: green; border-bottom-style: solid;"></div>
    </form>
        <script language="javascript" type="text/javascript">
        var _lat, _long;
        
    function CloseForm()
    {
        window.opener.document.SetValues(_lat,_long);
        window.opener.focus();
        window.close();
    }
        
    function LatLongCallback(point)
    {
        var resultMessageElem = document.getElementById("resultMessage");
        var resultMessageElem2 = document.getElementById("resultMessage2");
        var mapNoticeElem = document.getElementById("mapNotice");
        var mapElem = document.getElementById("map");
        
        if (point != null)
        {
            _lat = point.y;
            _long = point.x;
            resultMessageElem.innerText = "Yupi!! We found your Lat/Long to be "+point.x + " "+point.y;
            resultMessageElem2.style.display = "block";
            mapNoticeElem.style.display = "block";
            mapElem.style.display = "block";
            loadMap(_lat,_long);
        }
        else
        {
            _lat = 0;
            _long = 0;
            resultMessageElem.innerText = "Ooops... we couldn't find your Lat/Long. You can try it again using a sligthly variation of your address.";
            resultMessageElem2.style.display = "block";
            mapNoticeElem.style.display = "none";
            mapElem.style.display = "none";
        }
    }
    
    function PostalAddress2LatLong()
    {
        var geocoder = new GClientGeocoder();
        var elem = document.getElementById('postalAddress');
        postalAddressValue = elem.value;
        geocoder.getLatLng(postalAddressValue, LatLongCallback);
        return false;
    }
        </script>
</body>
</html>
